import store from "../store/index.js"
// #ifdef H5
const baseUrl = '';
// #endif

// #ifdef MP-WEIXIN

let baseUrl = '';
// const baseUrl = 'http://192.168.0.105:8080'
// #endif
export default function $request(options){
	let {
		method = 'GET',
		url,
		data,
		headers
	} = options
	const dataObj = {
		...data
	}
	let Authorization = undefined
	Authorization = 'Bearer ' + uni.getStorageSync('Authorization')
	const header = {
		Authorization: Authorization,
		'Accept-Version': '1.0.0',
		...headers
	}
	if(url.includes('https://')){
		baseUrl = ''
	}
	return new Promise((resolve, reject) => {
		uni.request({
			method,
		    url: baseUrl + url,
		    data,
		    header,
		    success: (res) => {
				if(window.location.href.includes('article/article')){
				}else{
					redirectLogin(res.data)
				}
				
				resolve(res.data);
		    },
			fail: (e) => {
			    console.log(e)
				if(url.includes('article/article')){
				}else{
					redirectLogin(e)
				}
				uni.showToast({
					title: e.message,
					icon:'none',
					duration: 2000
				});
				reject(e);
			}
		})
	})
}

export const redirectLogin = (res) => {
	// 登录状态失效
	if (res.code == 401 || res.code == 403) {
		console.log('登录状态已失效')
		// uni.showToast({
		// 	title: '请先进行登录',
		// 	icon:'none',
		// 	duration: 2000
		// });
		// uni.clearStorageSync();
		uni.removeStorageSync('Authorization');
		uni.removeStorageSync('projectDetail');
		uni.removeStorageSync('LoginInfo');
		uni.redirectTo({
			url: '/pages/login/login'
		});
	}
}